﻿using KingerRobot.MexicoSailunMDC.Dao;

namespace KingerRobot.MexicoSailunWMS.SpiService;

public class BBuildingManageImpl : IBuildingManageService
{
    private readonly static IRcsEquipBuilding _RcsEquipBuilding = new BRcsEquipBuilding();
    private readonly static IProdMaterial _ProdMaterial = new BProdMaterial();
    private readonly static IRcsEquip _RcsEquip = new BRcsEquip();
    private readonly static IWmsBuildingMaterialNotInWare _WmsBuildingMaterialNotInWare = new BWmsBuildingMaterialNotInWare();
    private readonly static IWmsBuildingNotInWareStatus _WmsBuildingNotInWareStatus = new BWmsBuildingNotInWareStatus();


    public List<BuildingBean> GetAllEquipBuildings()
    {
        var buildingList = new List<BuildingBean>();
        //所有成型机
        var buildings = _RcsEquipBuilding.GetAllEquipBuildings();
        //所有成型在产规格
        var buildingMaterials = _WmsBuildingNotInWareStatus.GetAllBuildingMaterialNotInWare();

        //所有物料
        var materialIds = buildings.Select(t => t.LineMaterialId).ToList();
        var materials = _ProdMaterial.GetMaterialListByIds(materialIds);
        //所有设备
        var equipIds = buildings.Select(t => t.Id).ToList();
        var equips = _RcsEquip.GetRcsEquipByIds(equipIds);

        buildings.Join(equips, f => f.Id, s => s.Id, (f, s) => new
        {
            Buildings = f,
            EquipName = s.EquipName,
            EquipCode = s.EquipCode,
            EquipEnable = s.EquipEnable,
        })
        //.GroupJoin(materials, f => f.Buildings.LineMaterialId, s => s.Id, (f, s) => new
        //{
        //    Buildings = f,
        //    Materials = s,
        //}).SelectMany(f => f.Materials.DefaultIfEmpty(), (f, s) => new
        //{
        //    Buildings = f.Buildings,
        //    MaterialsName = s?.MaterialName ?? "",
        //})
        .GroupJoin(buildingMaterials, f => f.Buildings.MesEquipId, s => s.BuildingCode, (f, s) => new
        {
            Buildings = f,
            BuildingMaterials = s,
        })
        .SelectMany(f => f.BuildingMaterials.DefaultIfEmpty(), (f, s) => new
        {
            Buildings = f.Buildings,
            MaterialsName = s?.EquipMaterial ?? "",
        })

        .ToList().ForEach(item =>
        {
            buildingList.Add(new()
            {
                Id = item.Buildings.Buildings.Id,
                EquipCode = item.Buildings.EquipCode,
                EquipEnable = item.Buildings.EquipEnable == 1 ? true : false,
                EquipName = item.Buildings.EquipName,
                CrashStatus = item.Buildings.Buildings.CrashStatus == 0 ? "否" : "是",
                CrashTime = item.Buildings.Buildings.CrashTime,
                UpdateTime = item.Buildings.Buildings.UpdateTime,
                LineMaterialName = item.MaterialsName,
                //LineTime = item.Buildings.Buildings.LineTime,
                Status = item.Buildings.Buildings.CrashStatus != 0,
            });
        });
        return buildingList;
    }

    //public List<BuildingBean> GetAllEquipBuildingsByStatus(bool status)
    //{

    //    int carshStatus = status ? 1 : 0;
    //    var buildingList = new List<BuildingBean>();
    //    //所有成型机
    //    var buildings = _RcsEquipBuilding.GetAllEquipBuildings();
    //    buildings=buildings.Where(t => t.CrashStatus == carshStatus).ToList();
    //    //所有物料
    //    var materialIds = buildings.Select(t => t.LineMaterialId).ToList();
    //    var materials = _ProdMaterial.GetMaterialListByIds(materialIds);
    //    //所有设备
    //    var equipIds = buildings.Select(t => t.Id).ToList();
    //    var equips = _RcsEquip.GetRcsEquipByIds(equipIds);

    //    buildings.Join(equips, f => f.Id, s => s.Id, (f, s) => new
    //    {
    //        Buildings = f,
    //        EquipName = s.EquipName,
    //        EquipCode = s.EquipCode,
    //        EquipEnable = s.EquipEnable,
    //    }).GroupJoin(materials, f => f.Buildings.LineMaterialId, s => s.Id, (f, s) => new
    //    {
    //        Buildings = f,
    //        Materials = s,
    //    }).SelectMany(f => f.Materials.DefaultIfEmpty(), (f, s) => new
    //    {
    //        Buildings = f.Buildings,
    //        MaterialsName = s?.MaterialName ?? "",
    //    }).ToList().ForEach(item =>
    //    {
    //        buildingList.Add(new()
    //        {
    //            Id = item.Buildings.Buildings.Id,
    //            EquipCode = item.Buildings.EquipCode,
    //            EquipEnable = item.Buildings.EquipEnable == 1 ? true : false,
    //            EquipName = item.Buildings.EquipName,
    //            CrashStatus = item.Buildings.Buildings.CrashStatus == 0 ? "否" : "是",
    //            CrashTime = item.Buildings.Buildings.CrashTime,
    //            UpdateTime = item.Buildings.Buildings.UpdateTime,
    //            LineMaterialName = item.MaterialsName,
    //            LineTime = item.Buildings.Buildings.LineTime,
    //           // Status= item.Buildings.Buildings.CrashStatus == 0 ? false : true,
    //        });
    //    });
    //    return buildingList;
    //}

    public int UpdateCrash(UpdateStatusParam param)
    {
        if (param is null)
        {
            return -1;
        }
        var result = _RcsEquipBuilding.UpdateEquipCrashStatusById(param.id);
        return result;
    }


    public int UpdateCrashStatus(UpdateBuildingParam param)
    {
        if (param is null)
        {
            return -1;
        }
        var syncChange = param.SyncChange;
        var result = _RcsEquipBuilding.UpdateCrashStatus(syncChange);
        return result;
    }


    public List<PoWmsBuildingMaterialNotInWare> GetAllBuildingMaterialNotInWare()
    {
        var result = _WmsBuildingMaterialNotInWare.GetAllBuildingMaterialNotInWare();
        return result;
    }
    public int AddNewBuildingMaterialNotInWare(BuildingMaterialNotInWareParam param)
    {
        var result = _WmsBuildingMaterialNotInWare.AddNewBuildingMaterialNotInWare(param);
        return result;
    }
    public int DeleteNewBuildingMaterialNotInWare(long id)
    {
        var result = _WmsBuildingMaterialNotInWare.DeleteNewBuildingMaterialNotInWare(id);
        return result;
    }






}
